package de.tabreaderandroid.tabreader.model;

import java.util.List;

import de.tabreaderandroid.tabreader.utils.InvalidTabFormatException;

/**
 * Holds all values that all elements in a tab file have in common.
 * 
 * @author André Stannek
 * 
 */
public abstract class TabFileElement implements Comparable<TabFileElement> {

	private int originIndex;

	/**
	 * Creates a new {@link TabFileElement} with an origin index.
	 * 
	 * @param originIndex
	 *            the origin index is the index of the line the element
	 *            occurred. If the element is multiline, it is the index of the
	 *            first line.
	 */
	public TabFileElement(int originIndex) {
		super();
		this.originIndex = originIndex;
	}

	/**
	 * The origin index is the index of the line the element occurred. If the
	 * element is multiline, it is the index of the first line.
	 * 
	 * @return the origin index
	 */
	public int getOriginIndex() {
		return originIndex;
	}

	/**
	 * @param originIndex
	 *            sets the origin index. The origin index is the index of the
	 *            line the element occurred. If the element is multiline, it is
	 *            the index of the first line.
	 */
	public void setOriginIndex(int originIndex) {
		this.originIndex = originIndex;
	}

	/**
	 * Processes the content of this element and returns it as a list of lines,
	 * where each lines is at most as long as the given line length.
	 * 
	 * @param maxLineWidth
	 *            the maximum width of a line in chars
	 * @return the lines that display the content
	 * @throws InvalidTabFormatException
	 *             if processing of the tab content is erroneous
	 */
	public abstract List<String> getLines(final int maxLineWidth)
			throws InvalidTabFormatException;

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(TabFileElement another) {
		return originIndex - another.originIndex;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + originIndex;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TabFileElement other = (TabFileElement) obj;
		if (originIndex != other.originIndex)
			return false;
		return true;
	}
}